﻿
using System.Collections.Generic;
using DevExpress.XtraTreeList;
using DevExpress.XtraTreeList.Nodes;
using HuaCheng.Model;
using System;
namespace HuaCheng.Controller
{
    public class SlopeControl
    {
        /// <summary>
        /// 根据TreeList生成斜坡工况列表
        /// </summary>
        /// <param name="treeList"></param>
        /// <returns></returns>
        public static List<SlopeStep> GetSlopeStepByTreeList(TreeList treeList)
        {
            List<SlopeStep> slopeList = new List<SlopeStep>();
            //遍历treeList节点
            for (int i = 0; i < treeList.Nodes.Count; i++)
            {
                SlopeStep slopeStep = GetSlopeStepByTreeListNode(treeList, i);
                slopeList.Add(slopeStep);
            }
            return slopeList;
        }
        /// <summary>
        /// 提取treeList节点为SlopeStep对象
        /// </summary>
        /// <param name="treeList"></param>
        /// <param name="i"></param>
        /// <returns></returns>
        private static SlopeStep GetSlopeStepByTreeListNode(TreeList treeList, int i)
        {
            SlopeStep slopeStep = new SlopeStep();
            //获取slopeStep的属性
            Type type = slopeStep.GetType();
            //遍历treelist的每一列
            for (int j = 0; j < treeList.Columns.Count; j++)
            {
                string strColumn = string.Empty;
                object copyItem = treeList.GetRowCellValue(treeList.Nodes[i], treeList.Columns[j]);
                if (copyItem != null)
                {
                    strColumn = Convert.ToString(copyItem);
                }
                //给每个属性赋表中对应单元格的值
                type.GetProperty(treeList.Columns[j].FieldName).SetValue(slopeStep, strColumn);
            }
            return slopeStep;
        }
        /// <summary>
        /// 根据斜坡工况列表生成TreeList
        /// </summary>
        /// <param name="stepList"></param>
        /// <param name="treeList"></param>
        /// <returns></returns>
        public static TreeList SetTreeListBySlopeList(List<SlopeStep> slopeList, TreeList treeList)
        {
            treeList.ClearNodes();
            //遍历工步信息列表
            for (int i = 0; i < slopeList.Count; i++)
            {
                SlopeStep slopeStep = slopeList[i];
                treeList = SetTreeListNodeBySlopeStep(slopeStep, treeList, i);
            }
            return treeList;
        }
        /// <summary>
        /// 根据SlopeStep对象生成TreeList节点
        /// </summary>
        /// <param name="slopeStep"></param>
        /// <param name="treeList"></param>
        /// <param name="i"></param>
        /// <returns></returns>
        private static TreeList SetTreeListNodeBySlopeStep(SlopeStep slopeStep, TreeList treeList, int i)
        {
            //新增表节点
            TreeListNode tmpNode = treeList.Nodes.Add();
            //获取slopeStep属性
            Type type = slopeStep.GetType();
            for (int j = 0; j < treeList.Columns.Count; j++)
            {
                if (type.GetProperty(treeList.Columns[j].FieldName).GetValue(slopeStep) != null)
                {
                    treeList.SetRowCellValue(tmpNode, treeList.Columns[j], Convert.ToString(type.GetProperty(treeList.Columns[j].FieldName).GetValue(slopeStep)));
                }
            }
            treeList.SetNodeIndex(tmpNode, i);
            return treeList;
        }
    }
}
